import numpy as np
from scipy.optimize import minimize


def create_feature_functions(data, labels):
    features = []
    for x, y in zip(data, labels):
        f = lambda w, x=x, y=y: np.dot(w, x) * (1 if y == 1 else -1)
        features.append(f)
    return features


def max_entropy_objective(w, feature_functions, regularization):
    scores = np.array([f(w) for f in feature_functions])
    log_likelihood = np.sum(np.log(1 + np.exp(-scores)))
    return log_likelihood + regularization * np.sum(w**2)


if __name__ == "__main__":
    # 示例数据
    X = np.array([[1, 2], [2, 1], [3, 3], [4, 5], [5, 4]])
    y = np.array([1, -1, 1, 1, -1])
    
    # 创建特征函数
    feature_functions = create_feature_functions(X, y)
    
    # 设置正则化参数
    regularization = 0.1
    
    # 初始化权重
    initial_weights = np.zeros(X.shape[1])
    
    # 使用BFGS算法优化目标函数
    result = minimize(max_entropy_objective, initial_weights, args=(feature_functions,
  regularization),
                      method='BFGS')
    
    # 输出结果
    print("Optimized weights:", result.x)
    print("Objective value at optimized weights:", result.fun)
